---Learning Ordinal Numbers----

local storyboard = require("storyboard")
local scene = storyboard.newScene()

local widget = require("widget")

local status, 		--game status
	group, 			--the main group
	ordinal,		--the ordinal numbers(1st, 2nd...)
	--score,			--the score
	count,			--the number of questions/rounds
	answers,		--student's answers
	instruction,	--the instruction
	title,			--the game's title
	round,			--the count of question(s) asked
	front,
	me, items, key, image,round,status
	
function moveon()
	items = math.random(3,10)
	key = math.random(1,items)
	image = math.random(1,9)
end

function scene:createScene(event)
	--group = self.view
	group = display.newGroup()
	
	background = display.newImageRect( "images/bg22.jpg",  display.contentWidth,  display.contentHeight)
	background:setReferencePoint( display.TopLeftReferencePoint )
	background.x, background.y = 0, 0
	
	group:insert(background)
	group:setReferencePoint( display.CenterReferencePoint )
	--set the screen size
	--display.contentWidth, display.contentHeight = display.contentHeight, display.contentWidth
	--the ordinal	
	ordinal = {{'1st','first'},{'2nd','second'},{'3rd','third'},{'4th','fourth'},{'5th','fifth'},{'6th','sixth'},{'7th','seventh'},{'8th','eighth'},{'9th','ninth'},{'10th','tenth'}}
	images = {{'apple.png', 'apple'},{'book.png', 'book'},{'umbrella.png', 'umbrella'},{'car.png', 'car'},{'cup.png', 'cup'},{'fish2.png', 'fish'},{'hat.png', 'hat'},{'school-bus.png', 'school bus'},{'sun.png', 'sun'}}
	score = 0
	count = 5
	round = 0
	status = 1
	
	title = display.newText("Learning Ordinal Numbers", 0,0, font, 18)
	title:setReferencePoint(display.CenterReferencePoint)
	title .x = display.contentWidth * .5
	title .y = display.contentHeight * .2
	title:setTextColor(96, 17, 25)
	group:insert(title)
	
	scoreText = display.newText("Score: "..0, 0,0, font, 18)
	scoreText .x = display.contentWidth * .2
	scoreText .y = display.contentHeight * .25
	scoreText:setTextColor(96, 17, 25)
	group:insert(scoreText)
		
	instruction = display.newText("", 0, 0, font, 24)
	instruction:setReferencePoint(display.CenterReferencePoint)
	instruction.x = display.contentWidth * .5
	instruction.y = display.contentHeight * .35
	instruction:setTextColor(5, 61, 32)
	group:insert(instruction)
	
	box = display.newRect(0, 0, display.contentWidth - 18, display.contentHeight - 380)
	box:setFillColor(50, 50, 50)
	box:setStrokeColor(255, 255, 255)
	box.strokeWidth = 1
	box.alpha = 0.2
	box:setReferencePoint(display.TopCenterReferencePoint)
	box.x = display.contentWidth * 0.5
	box.y = display.contentHeight * .35
	group:insert(box)
	
	moveon()
end

function scene:enterScene(event)
	--storyboard.purgeScene( "first" )
	storyboard.purgeScene("start")
	storyboard.purgeScene("gameover")
	
	--called after 'count' rounds
	function finish()
		front:removeSelf()
		
		-- scoreText:removeSelf()
		 front = display.newGroup()
		instruction:setText("Your Score is "..score, 0, 0, native.systemFont, 72)
		instruction.x = display.contentWidth * 0.5
		if(system.orientation == "portrait" or system.orientation == "portraitUpsideDown") then
			instruction.y = display.contentHeight * 0.50
		else
			instruction.y = display.contentHeight * 0.55
		end
		instruction:setTextColor(255,255,255)
		box.alpha = 1
		box.width = 260
		box.height=100
		front:insert(box)
		front:insert(instruction)
		status = 0 --end of game 
		
		--timer.performWithDelay(1000,  function() group:removeSelf() storyboard.gotoScene( "games.gameover", "zoomOutInFade", 500 ) end , 1)
	end
	--the function to catch the student's answer
	local answerMe = function(event)
		local imageId = event.target.id
		if(imageId == key) then
			score = score + 100
			scoreText.text = "Score: "..score
		else
			score = score - 10
			scoreText.text = "Score: "..score
			round = round - 1
		end
		for i = 1, items do
			group:remove(me[i])
		end
		moveon()
		
		timer.performWithDelay(500, start , 1)
	end
	
	function start()
		front = display.newGroup()
		group:insert(front)
		round = round + 1
		if(round <= count) then
			local x, i, y
			w = (display.contentWidth - 50) / items
			me = {}
			box.height = w+40
			group:insert(box)
			--instruction:setText("Click the "..ordinal[key][2].." "..images[image][2])
			instruction.text ="Click the "..ordinal[key][2].." "..images[image][2]
			if(items <= 5) then x = 55 else x= 40 end
			y = display.contentHeight * 0.52
			--scoreText.x = display.contentWidth * .1
			--scoreText.y = display.contentHeight * 0.1
			for i = 1, items, 1 do
				me[i] = display.newImageRect( "images/"..images[image][1], w, w, true )
				me[i].x = x + (w*(i-1))
				me[i].y = y
				me[i].id = i
				me[i]:setReferencePoint(display.TopLeftReferencePoint)
				me[i]:addEventListener("tap", answerMe)
				group:insert(me[i])
			end
		else
			finish()
		end
		
	end
	
	start()
end

function scene:exitScene(event)
	
end

function scene:destroyScene(event)

end

scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)

return scene